<template>
  <Popup
      :round="round"
      v-model="visible"
      :overlay="true"
      :transition-appear="true"
      :close-on-popstate="true"
      overlay-class="layer_wrap"
      @click-overlay="handleClose"
      :safe-area-inset-bottom="true"
      :close-on-click-overlay="false"
  >
    <div class="report-dept">
      <div class="header-area">
        <span>请选择医生信息</span>
      </div>
      <div class="content-wrap">
        <el-row :gutter="30" class="content-area-inside" :style="getStyle('inside')">
          <template v-if="!!(info?.DRList?.length)">
            <template v-for="item in info.DRList">
              <el-col :span="12" data-dept="患者号别">
                <div class="item" @click="handleClickDr(item)">
                  {{ item.workerName }}{{ onlineStatus(item) }}
                </div>
              </el-col>
            </template>
          </template>
          <template v-else>
            <el-empty description="暂无数据"></el-empty>
          </template>
        </el-row>
      </div>
      <div class="footer-area">
        <div class="patient-info">您好,{{ patientInfo.patientName }}</div>
        <div class="countdown" @click="handleClose(false)">
          退出&nbsp;{{ `${time / 1000}` }}s
        </div>
      </div>
    </div>
  </Popup>
</template>

<script>
import {Button, Divider, Popup, Sidebar, SidebarItem} from 'vant';
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";

export default {
  name: 'ReportDR',

  componentName: "ReportDR",

  props: {
    visible: {
      type: Boolean,
      required: false
    },
    info: {
      type: Object,
      required: true
    },
  },

  components: {
    Popup,
    Button,
    Divider,
    Sidebar,
    SidebarItem
  },

  data() {
    return {
      activeKey: 0,
      round: true,
      time: 60000,
      timer: null,
      loading: false,
      patientInfo: Object(),
    }
  },

  computed: {
    // 行内样式
    getStyle() {
      return function (row) {
        let obj = Object();
        switch (row) {
          case 'inside':
            let winHeight;
            // 获取窗口宽度
            if (window.innerWidth) {
              winHeight = window.innerHeight;
            } else if ((document.body) && (document.body.clientHeight)) {
              winHeight = document.body.clientHeight;
            }
            Reflect.set(obj, 'maxHeight', pxToVw(winHeight - 176 - 150 - 200))
            return obj;
        }
      }
    },

    // 医生在线状态中文转化
    onlineStatus() {
      return function (row) {
        let type = parseInt(row.onlineStatus);
        switch (type) {
          case 0:
            return '(在线)'
          case 2:
            return '(离线)'
          default:
            return '(未知)'
        }
      }
    }

  },

  mounted() {
    console.log('init')
  },

  methods: {
    // 关闭弹层
    handleClose(statu) {
      const self = this;
      self.$emit('handleCancel')
      self.$emit('handle-cancel')
      self.loading = true;
      clearInterval(self.timer);
      if (!statu) {
        setTimeout(() => {
          self.loading = false;
        }, 1500)
      } else {
        self.loading = false;
      }
    },

    // 点击选中医生-更改号别类型
    handleClickDr(row) {
      let {registerObjId,registerId,registerObjName,registerObjType} = this.patientInfo;
      Reflect.set(row, 'registerObjId', registerObjId);
      Reflect.set(row, 'registerId', registerId);
      Reflect.set(row, 'registerObjName', registerObjName);
      Reflect.set(row, 'registerObjType', registerObjType);
      this.$emit('handle-sure', row)
      this.$emit('handleSure', row);
      this.handleClose(false);
    },
  },

  watch: {
    visible: {
      handler: function (val) {
        const self = this;
        let {patientInfo, time} = this.info;
        self.widgetKey = uuid();
        if (val) {
          self.patientInfo = patientInfo;
          self.time = time * 1000;
          self.timer = setInterval(() => {
            self.time = (self.time - 1000);
            if ((self.time) === 0) {
              self.handleClose(true);
              clearInterval(self.timer)
            }
          }, 1000);
        } else {
          clearInterval(self.timer);
        }
      },
      deep: true,
      immediate: true
    },
  },

}
</script>

<style lang="scss" scoped>
@import "./assets/style";
</style>
